const router = require('koa-router')();
const fs = require('fs');
const path = require('path');

function getfile(dirname) {
    let files = fs.readdirSync(dirname);
    return files.filter(item => {
        return item.endsWith('.js') && item !== 'index.js'
    })
}

function reqfile(filesjs) {
    filesjs.map(item => {
        let obj = require(path.join(__dirname, item))
        for (let o in obj) {
            let reqmsg = o.split(' ');
            let method = reqmsg[0];
            let url = reqmsg[1];
            if (method === 'get') {
                router.get(url, obj[o]);
            } else if (method === 'post') {
                router.post(url, obj[o]);
            } else if (method === 'delete') {
                router.delete(`${url}`, obj[o]);
            } else if (method === 'put') {
                router.put(`${url}`, obj[o]);
            }
        }
    })

    return router.routes();
}

module.exports = () => {
    return reqfile(getfile(__dirname));
}